"""
单例模式：让一个类只能创建出唯一的实例
"""
from functools import wraps
from threading import RLock


def singleton(cls):
    instances = {}
    lock = RLock()

    @wraps(cls)
    def wrapper(*args, **kwargs):
        if cls not in instances:
            with lock:
                if cls not in instances:
                    instances[cls] = cls(*args, **kwargs)
        return instances[cls]

    return wrapper


@singleton
class President:

    def __init__(self, name):
        self.name = name

    def __str__(self):
        return self.name


p1 = President('骆昊')
p2 = President('王大锤')
print(p1, p2)
print(p1 == p2)
print(id(p1), id(p2))
